package hlhj.fhp.supreme.customview

import android.app.Activity
import android.app.Dialog
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.provider.MediaStore
import android.support.v4.app.ActivityCompat
import android.widget.Toast
import fhp.hlhj.giantfold.utils.ToastUtils
import hlhj.fhp.supreme.R
import kotlinx.android.synthetic.main.downimg_dialog.*
import android.support.v4.app.ActivityCompat.requestPermissions
import android.support.v4.content.ContextCompat
import android.R.attr.bitmap
import android.net.Uri
import android.os.Build
import java.nio.file.Files.exists
import android.os.Environment.getExternalStorageDirectory
import android.os.Build.BRAND
import android.os.Environment
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException


/**
 * Created by Administrator on 2018\6\19 0019.
 */
class DownImgDialog(context: Context,private val type:Int) :Dialog(context, R.style.CustomDialog){
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.downimg_dialog)
        val PERMISSIONS = arrayOf("android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE")
//检测是否有写的权限
        val permission = ContextCompat.checkSelfPermission(context,
                "android.permission.WRITE_EXTERNAL_STORAGE")
        if (permission != PackageManager.PERMISSION_GRANTED) {
            // 没有写的权限，去申请写的权限，会弹出对话框
            ActivityCompat.requestPermissions(context as Activity, PERMISSIONS, 1)
        }

        btSave.setOnClickListener {
            when(type){
                1->{
                    saveBitMap(BitmapFactory.decodeResource(context.resources,R.mipmap.ic_qr),"二维码下载")
//                    MediaStore.Images.Media.insertImage(context.contentResolver,BitmapFactory.decodeResource(context.resources,R.mipmap.ic_qr),"二维码","下载")
                    ToastUtils.toast(context,"保存成功,请使用微信扫描您相册中的二维码")
/*                    try {
                        val intent = Intent()
                        intent.setComponent(ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI"))
                        intent.putExtra("LauncherUI.From.Scaner.Shortcut", true)
//            intent.setFlags(335544320)
                        intent.setAction("android.intent.action.VIEW")
                        context.startActivity(intent)
                    } catch (e: Exception) {
                    }*/
                    dismiss()
                }
                0->{
                    saveBitMap(BitmapFactory.decodeResource(context.resources,R.mipmap.kefu_weixin),"二维码客服")
//                    MediaStore.Images.Media.insertImage(context.contentResolver,BitmapFactory.decodeResource(context.resources,R.mipmap.kefu_weixin),"二维码","下载")
                    ToastUtils.toast(context,"保存成功,请使用微信扫描您相册中的二维码")
 /*                   try {
                        val intent = Intent()
                        intent.setComponent(ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI"))
                        intent.putExtra("LauncherUI.From.Scaner.Shortcut", true)
//            intent.setFlags(335544320)
                        intent.setAction("android.intent.action.VIEW")
                        context.startActivity(intent)
                    } catch (e: Exception) {
                    }*/
                    dismiss()
                }
            }

        }
    }
    fun saveBitMap(bitmap: Bitmap,bitName:String){
        val fileName: String
        val file: File
        if (Build.BRAND.equals("Xiaomi")) { // 小米手机
            fileName = Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/" + bitName
        } else {  // Meizu 、Oppo
            fileName = Environment.getExternalStorageDirectory().getPath() + "/DCIM/" + bitName
        }
        file = File(fileName)
        file.setReadable(true)
        if (file.exists()) {
            file.delete()
        }
        val out: FileOutputStream
        try {
            out = FileOutputStream(file)
            // 格式为 JPEG，照相机拍出的图片为JPEG格式的，PNG格式的不能显示在相册中
            if (bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out)) {
                out.flush()
                out.close()
                // 插入图库
                MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), bitName, null)

            }
        } catch (e: FileNotFoundException) {
            e.printStackTrace()
        } catch (e: IOException) {
            e.printStackTrace()

        }

        // 发送广播，通知刷新图库的显示
        context.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + fileName)))
    }
}